package com.wangsd.common.intercepter;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    /**
     * 插入填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasSetter("createdTime")) {
            setFieldValByName("createdTime", new Date(), metaObject);
        }
        //逻辑删除字段
        if (metaObject.hasSetter("deleted")) {
            setFieldValByName("deleted", 0, metaObject);
        }


    }

    /**
     * 修改填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("updatedTime")) {
            Object val = this.getFieldValByName("updatedTime", metaObject);
            if (val == null) {
                this.setFieldValByName("updatedTime", new Date(), metaObject);
            }
        }

    }
}
